package com.hzx.demo.company;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import com.hzx.demo.LXML.Entity.ControlEntity;
import com.hzx.demo.LXML.LXML;
import com.hzx.demo.R;
import com.hzx.demo.util.Entry;
import com.hzx.demo.util.HZXLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;

/**
 * Created by ldkxingzhe on 2016/5/30.
 */
@Entry(desc = "公司测试使用", createTime = "2016-05-30 16:19")
@EActivity(R.layout.activity_socket)
public class SocketActivity extends AppCompatActivity implements SocketHelper.Listener {
    @SuppressWarnings("unused")
    private static final String TAG = "SocketActivity";
    private SocketHelper mSocketHelper;
    private String mSessionId;
    @AfterViews
    public void afterView(){
        new Thread(){
            @Override
            public void run() {
                mSocketHelper = new SocketHelper();
                mSocketHelper.setListener(SocketActivity.this);
            }
        }.start();
    }

    @Click(R.id.tv_login)
    public void login(){
        HZXLog.v(TAG, "login");
        try {
            mSocketHelper.sendMessage(LXML.toXML(new ControlEntity.LoginControlEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @UiThread
    protected void onLoginResult(ControlEntity controlEntity, String message){
        if("0".equals(controlEntity.getResult_code())){
            mSessionId = controlEntity.getSession_id();
            mSocketHelper.setSessionId(mSessionId);
            Toast.makeText(SocketActivity.this, "登录成功, sessionId is " + mSessionId, Toast.LENGTH_SHORT).show();
            String devInfoMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?><msg><head><msg_id>msg_multi_dev_info_get</msg_id>" +
                    "<msg_code>100</msg_code><msg_time></msg_time><from></from><to>" +
                    "</to><verification_code></verification_code><session_id>"+
                    mSessionId +
                    "</session_id></head><body><group_id>0</group_id></body></msg>";
            mSocketHelper.sendMessage(devInfoMessage);
        }else{
            Toast.makeText(SocketActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Click(R.id.tv_left)
    public void left(){
        onPositionClick("1");
    }

    private void onPositionClick(String cmd){
        ControlEntity.PositionControlEntity positionControlEntity = new ControlEntity.PositionControlEntity();
        positionControlEntity.setSession_id(mSessionId);
        positionControlEntity.setCmd(cmd);
        positionControlEntity.setDev_id("7");
        positionControlEntity.setSpeed("5");
        positionControlEntity.setTime("3");
        try {
            mSocketHelper.sendMessage(LXML.toXML(positionControlEntity));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @UiThread
    protected void onPositionClickResult(ControlEntity controlEntity, String message){
        HZXLog.v(TAG, "onPositionClickResult, and message is %s.", message);
        showDialog(controlEntity);
    }

    private void showDialog(ControlEntity entity){
        if("0".equals(entity.getResult_code())){
            new AlertDialog.Builder(SocketActivity.this)
                    .setTitle("成功")
                    .setMessage("指令" + entity.getMsg_id())
                    .setPositiveButton("确认", null)
                    .create().show();
        }else{
            new AlertDialog.Builder(SocketActivity.this)
                    .setTitle("失败")
                    .setMessage(entity.getResult_msg())
                    .setPositiveButton("确认", null).create().show();
        }
    }

    @Click(R.id.tv_right)
    public void right(){
        onPositionClick("2");
    }
    @Click(R.id.tv_up)
    public void up(){
        onPositionClick("3");
    }
    @Click(R.id.tv_down)
    public void down(){
        onPositionClick("4");
    }

    @Override
    protected void onDestroy() {
        mSocketHelper.close();
        super.onDestroy();
    }

    @Override
    public void onMessageCome(String message) {
        HZXLog.v(TAG, "onMessageCome, and message is %s.", message);
        try {
            ControlEntity controlEntity = LXML.fromXML(message, ControlEntity.class);
            String msgId = controlEntity.getMsg_id();
            if("msg_login".equals(msgId)){
                onLoginResult(controlEntity, message);
            }else{
                onPositionClickResult(controlEntity, message);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
